package com.example.shkjdemo.picker

import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.contrarywind.listener.OnItemSelectedListener
import com.contrarywind.view.WheelView
import com.example.shkjdemo.R

class PickerActivity : AppCompatActivity() {
    lateinit var wheelView: WheelView
    lateinit var wheelViewRight: WheelView
    lateinit var tvLeft: TextView
    lateinit var tvRight: TextView
    private val mOptionsItems: MutableList<String> = arrayListOf()
    private val mOptionsItemsRight: MutableList<String> = arrayListOf()
    private val currentItemLeft = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_picker)
        wheelView = findViewById(R.id.wheelView)
        wheelViewRight = findViewById(R.id.wheelView2)
        tvLeft = findViewById(R.id.tvLeft)
        tvRight = findViewById(R.id.tvRight)
        wheelView.setCyclic(true)
        wheelView.setTextXOffset(1)
        wheelView.setItemsVisibleCount(3)
        wheelViewRight.setCyclic(true)
        wheelViewRight.setItemsVisibleCount(7)
        for (i in 0..4) {
            mOptionsItems.add("中文粤语")
        }

        for (i in 0..4) {
            if (i == currentItemLeft) {
                continue
            }
            mOptionsItemsRight.add("R item 8")
        }

        wheelView.adapter = ArrayWheelAdapter(mOptionsItems)
        wheelView.currentItem = currentItemLeft
        wheelViewRight.currentItem = 0

        wheelViewRight.adapter = ArrayWheelAdapter(mOptionsItemsRight)

        wheelView.setOnItemSelectedListener(object : OnItemSelectedListener {
            override fun onItemSelected(index: Int) {
                tvLeft.text = mOptionsItems[index]
                reAdapter(index)
                Log.v("PickerActivity", "onItemSelected index = $index")
            }

            override fun onStartSlide(index: Int) {
                Log.v("PickerActivity", "onStartSlide index = $index")
            }

        })

        wheelViewRight.setOnItemSelectedListener(object : OnItemSelectedListener {
            override fun onItemSelected(index: Int) {
                tvRight.text = mOptionsItemsRight[index]
                Log.v("PickerActivity", "onItemSelected index = $index")
            }

            override fun onStartSlide(index: Int) {
                Log.v("PickerActivity", "onStartSlide index = $index")
            }


        })
    }


    fun reAdapter(index: Int) {
        mOptionsItemsRight.clear()
        for (i in 0..20) {
            if (i == index) {
                continue
            }
            mOptionsItemsRight.add("R item 8")
        }
        wheelViewRight.adapter = ArrayWheelAdapter(mOptionsItemsRight)
    }
}
